﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using CubeDAL;
using SysInfo;
namespace CubePresentation
{
    /// <summary>
    /// Interaction logic for CrossQueryWindow.xaml
    /// </summary>
    public partial class CrossQueryWindow : Window
    {
        TableInfo tableInfo;
        const int MAX_CONDITION_NUM = 5;
        string dataBase;
        string tableName;
        public CrossQueryWindow(string database, string tablename, TableInfo ti)
        {
            InitializeComponent();
            tableInfo = ti;
            dataBase = database;
            tableName = tablename;
            MySQLWrapper dataSqlWrapper = new MySQLWrapper(Constant.DB_SERVER,
                Constant.DB_DATABASE,
                Constant.DB_USER,
                Constant.DB_PASSWORD);
            init();

        }
        private void init()
        {
            
            foreach (var pair in tableInfo[tableName])
            {
                comboBoxDisplayField.Items.Add(pair.Value.DisplayName);
            }
        }

        private void buttonAdd_Click_1(object sender, RoutedEventArgs e)
        {
            if (conditionGrid.Children.Count < MAX_CONDITION_NUM)
            {
                ConditionPanel cp = new ConditionPanel(dataBase, tableName, tableInfo);
                cp.SetValue(Grid.RowProperty, conditionGrid.Children.Count);
                cp.SetValue(Grid.ColumnProperty, 0);
                cp.SetValue(Grid.ColumnSpanProperty, conditionGrid.ColumnDefinitions.Count);
                conditionGrid.Children.Add(cp);
            }
        }

        private void buttonDel_Click_1(object sender, RoutedEventArgs e)
        {
            List<ConditionPanel> toDelete = new List<ConditionPanel>();
            foreach (ConditionPanel cp in conditionGrid.Children)
            {
                if (cp.Selected)
                {
                    toDelete.Add(cp);
                }
            }
            foreach (var cp in toDelete)
            {
                conditionGrid.Children.Remove(cp);
            }
            int row = 0;
            foreach (ConditionPanel cp in conditionGrid.Children)
            {
                cp.SetValue(Grid.RowProperty, row++);
            }
        }

    

        private void buttonQuery_Click_1(object sender, RoutedEventArgs e)
        {
            if (comboBoxDisplayField.SelectedIndex != -1)
            {
                List<string> fields = new List<string>();
                fields.Add(tableInfo[tableName].ElementAt(comboBoxDisplayField.SelectedIndex).Value.DBFieldName);
                CrossQuery.ConditionQuery.ConditionalQuery query = new CrossQuery.ConditionQuery.ConditionalQuery(dataBase, tableName, fields.ToArray());
                foreach (ConditionPanel cp in conditionGrid.Children)
                {
                    query.AddCondition(cp.Condition);
                }
                query.ToSQLCommand();
                labelToBeRemoved.Content = query.ToSQLCommand();
                
            }
        }

        private void comboBoxDisplayField_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {

        }

    }
}
